<?php
/**
 * Created by PhpStorm.
 * User: yandian
 * Date: 2017/4/22
 * Time: 01:08
 */

namespace app\weixin\controller;


use app\admin\controller\DonationComment;
use app\admin\controller\Login;
use app\common\controller\LiteBase;
use app\common\model\Member;
use app\common\model\MemberCoupon;
use app\common\model\MemberLuck;
use app\common\util\Assert;
use app\common\util\DateTimeUtil;
use app\common\util\LoginManager;


class Donation extends LiteBase
{
    private function getData()
    {
        $rows = \app\common\model\Donation::instance()
            ->gotoPage()
            ->where(['status' => 1, 'member_id' => LoginManager::getLoginUserId()])
            ->order('id desc')->select();
        foreach ($rows as &$row) {
            $row['money'] = number_format($row['money'] / 100, 2);
        }
        return $rows;
    }

    public function jsonList()
    {
        return $this->jsonSuccess($this->getData());
    }

    public function index()
    {
        $m = $this->getData();
        $this->assign('m', $m);
        return $this->fetch();
    }

    public function home()
    {
        return $this->fetch();
    }

    public function donate()
    {
        return $this->fetch();
    }

    public function donateOther()
    {
        return $this->fetch();
    }

    public function power()
    {
        return $this->fetch();
    }

    private function getDataOfMsg()
    {
        $rows = \app\common\model\DonationComment::instance()
            ->gotoPage()
            ->alias('d')
            ->join('member m', 'm.id=d.member_id')
            ->field('d.*,m.avatar,m.nickname')
            ->order('d.id desc')->select();
        foreach ($rows as &$row) {
            $row['create_time'] = formatTime($row['create_time']);
        }
        return $rows;
    }

    public function jsonListOfMsg()
    {
        return $this->jsonSuccess($this->getDataOfMsg());
    }

    public function msg()
    {
        $this->assign('m', $this->getDataOfMsg());
        return $this->fetch();
    }

    public function createOrder()
    {
        if (IS_POST) {
            return $this->wrapper(function () {
                $payment_id = intval(I('post.payment_id'));
                $m = intval(I('post.m'));
                if (!in_array($m, [5, 10, 50, 100, 500, 5000, 10000, 50000])) {
                    return [false, '捐赠的金额不合法'];
                }
                $now = DateTimeUtil::now();
                $mid = LoginManager::getLoginUserId();
                $rt = \app\common\model\Donation::instance()->add([
                    'member_id' => $mid,
                    'order_no' => ccnn_build_orderno(),
                    'status' => 0,
                    'money' => $m * 100,
                    'member_type' => intval(Member::instance()->where(['id' => $mid])->getField('auth_status')),
                    'pay_type' => $payment_id,
                    'create_time' => $now,
                ]);
                if ($rt) {
                    return [true, $rt];
                }
                return [false, '订单生成失败'];
            });
        }
    }

    public function payment()
    {
        LoginManager::doCheckLogin();
        $this->assign('m', number_format(I('get.m', 0), 2));
        return $this->fetch();
    }

    public function donateResult()
    {
        $id = I('get.id', 0, 'intval');
        if ($id) {
            $mid = LoginManager::getLoginUserId();
            $entry = \app\common\model\Donation::instance()->where(['id' => $id])->find();
            if (!empty($entry) && $entry['member_id'] == $mid) {
                $entry['money'] = number_format($entry['money'] / 100, 2);
                $entry['create_time'] = ccnn_dateformat2($entry['create_time']);
                $entry['coral_num'] = \app\common\model\Coral::instance()->where(['donation_id' => $id])->count();
                $entry['corals_num'] = \app\common\model\CoralReef::instance()->where(['donation_id' => $id])->count();
                $entry['certificate_num'] = \app\common\model\LoveCertificate::instance()->where(['donation_id' => $id])->count();
                $entry['luck_count'] = MemberLuck::instance()->where(['member_id' => $mid, 'donation_id' => $id, 'status' => 0])->count();
                $this->assign('m', $entry);
                return $this->fetch();
            }
        }
        $this->redirect('index/index');
    }

    public function comment()
    {
        if (IS_POST) {
            $data = Assert::notEmpty(I('post.'), ['content']);
            $data['create_time'] = DateTimeUtil::now();
            $data['member_id'] = LoginManager::getLoginUserId();
            \app\common\model\DonationComment::instance()->add($data);
            return $this->jsonSuccess();
        }
        return $this->fetch();
    }


}